import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.StringTokenizer;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileSystemView;


public class SkyrimUserChanger extends JFrame {

	public String chosenCharacterName;

	public class ComboBoxListener implements ActionListener {


		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			JComboBox cb = (JComboBox)arg0.getSource();
			chosenCharacterName = (String)cb.getSelectedItem();
			System.out.println("You chose "+chosenCharacterName);
		}

	}

	private class ButtonListener implements ActionListener {

		private JFileChooser fc;


		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			fc= new JFileChooser();
			fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			fc.setAcceptAllFileFilterUsed(false);
			//			fc.setCurrentDirectory(new java.io.File("."));
			if(arg0.getSource()== btnSaveLocation){
				int returnVal = fc.showOpenDialog(SkyrimUserChanger.this);

				if (returnVal == JFileChooser.APPROVE_OPTION) {
					btnNewCharacter.setEnabled(true);
					File dir = fc.getSelectedFile();
					chosenDir = dir.toString();
					String[] children = dir.list();
					if (children == null) {
						// Either dir does not exist or is not a directory
					}
					else {
						for (int i=0; i<children.length; i++) {
							// Get filename of file or directory
							//							if(!children[i].endsWith("bat")){
							characterList.add(children[i]);
							System.out.println(characterList.get(i));
							//								cmbBxCharacterList.addItem(characterList.get(i));
							//							}
						}

						for(int i = 0;i<characterList.size();i++){
							if(!characterList.get(i).endsWith("bat")){
								cmbBxCharacterList.addItem(characterList.get(i));
							}
						}

					}
				}
			}
			else if(arg0.getSource()==btnLaunch){
				dir2 = new File(dir2.getParent());
				String target = dir2.toString();
				System.out.println("Target: " + target);

				String source = chosenDir;
				String sourcePath = source + "\\" + chosenCharacterName;
				System.out.println("Source: " + sourcePath);

				String currentCharPath = source + "\\" + currentCharacter;
				System.out.println("Current: "+ currentCharPath);

				String batch = "@echo off\nxcopy "+target+" "+currentCharPath+" /s \nxcopy "+sourcePath+" "+target+" /s";
				System.out.println(batch);
				File batchFile = new File(source + "\\Switch_to_"+chosenCharacterName+".bat");
				System.out.println(batchFile.toString());
				try {
					batchFile.createNewFile();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				try {
					//					FileOutputStream fos = new FileOutputStream(batchFile);
					PrintStream ps = new PrintStream(batchFile);
					ps.print(batch);
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
			else if(arg0.getSource()==btnNewCharacter){

			}
		}

	}

	private String chosenDir;
	private JPanel contentPane;
	private JLabel lblCurrentCharacter;
	private JLabel label;
	private JComboBox cmbBxCharacterList;
	private JLabel lblChooseCharacter;
	private JButton btnSaveLocation;
	private JButton btnLaunch;
	private JLabel lblNewLabel;
	private JButton btnNewCharacter;
	private ArrayList<String> characterList;
	private String currentCharacter;
	private File dir2;
	private String myDocsFile;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		String osVersion = System.getProperty("os.name");
		System.out.println(osVersion);
		if(osVersion.equals("Windows 7")){
			EventQueue.invokeLater(new Runnable() {
				public void run() {
					try {
						SkyrimUserChanger frame = new SkyrimUserChanger();
						frame.setVisible(true);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
		}
		else{
			JOptionPane.showMessageDialog(null, "Not Windows 7!");
		}
	}

	/**
	 * Create the frame.
	 */
	public SkyrimUserChanger() {
		characterList = new ArrayList<String>();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 477, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		lblChooseCharacter = new JLabel("Choose Character:");
		lblChooseCharacter.setBounds(82, 124, 126, 14);
		contentPane.add(lblChooseCharacter);

		cmbBxCharacterList = new JComboBox();
		cmbBxCharacterList.setEditable(true);
		cmbBxCharacterList.setBounds(201, 121, 148, 20);
		contentPane.add(cmbBxCharacterList);
		cmbBxCharacterList.addActionListener(new ComboBoxListener());

		label = new JLabel("Current Character:");
		label.setBounds(82, 99, 167, 14);
		contentPane.add(label);

		lblCurrentCharacter = new JLabel("");
		lblCurrentCharacter.setForeground(Color.BLUE);
		lblCurrentCharacter.setBounds(201, 99, 148, 14);
		contentPane.add(lblCurrentCharacter);

		btnLaunch = new JButton("Launch Game");
		btnLaunch.setBounds(82, 209, 300, 23);
		contentPane.add(btnLaunch);
		btnLaunch.addActionListener(new ButtonListener());

		lblNewLabel = new JLabel("Skyrim Chacter Chooser");
		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setBounds(10, 11, 441, 38);
		contentPane.add(lblNewLabel);

		btnSaveLocation = new JButton("Choose Location of Saves");
		btnSaveLocation.setBounds(82, 53, 300, 23);
		contentPane.add(btnSaveLocation);
		btnSaveLocation.addActionListener(new ButtonListener());

		btnNewCharacter = new JButton("Make New Character");
		btnNewCharacter.setBounds(82, 175, 300, 23);
		contentPane.add(btnNewCharacter);
		btnNewCharacter.setEnabled(false);

		JFileChooser fr = new JFileChooser();
		FileSystemView fw = fr.getFileSystemView();
		myDocsFile = (fw.getDefaultDirectory()).toString();
		myDocsFile+="\\My Games\\Skyrim\\Saves";
		System.out.println(myDocsFile);

		dir2 = new File(myDocsFile);
		String[] children = dir2.list();
		if (children == null) {
			// Either dir does not exist or is not a directory
			System.out.println("wut");
		}
		else {
			for (int i=0; i<children.length; i++) {
				// Get filename of file or directory
				System.out.println(children[i]);
			}
			StringTokenizer st = new StringTokenizer(children[children.length-1], "-");
			while (st.hasMoreTokens()) {

				currentCharacter = st.nextToken();
				System.out.println(currentCharacter);
			}	

			st = new StringTokenizer(currentCharacter);
			currentCharacter = st.nextToken();
			System.out.println(currentCharacter);

			lblCurrentCharacter.setText(currentCharacter);
		}


	}	
}
